package com.uenpay.im.ui.old.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.TextView;

import com.qyx.android.database.FriendDB;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;
import com.uenpay.im.ui.old.activity.group.GroupMangerHandler;
import com.uenpay.im.callback.FragmentCallBack;
import com.uenpay.im.callback.OnGetAllClassmate;
import com.uenpay.im.configuration.ShareData;
import com.uenpay.im.fragment.ApplyFragment;
import com.uenpay.im.fragment.TalkListFragment;
import com.uenpay.im.invokeitems.friends.GetFriends;

/**
 * @author SL
 * @Create Date: 2014-12-7 上午11:31:43
 * @copyRight http://www.qiyunxin.com
 */
@Deprecated
public class TabMenuActivity extends FragmentActivity implements FragmentCallBack {

    private GroupMangerHandler groupMangerHandler = new GroupMangerHandler();
    private updatemsgcountBroadcastReceiver mBroadcastReceiver = null;
    public static FragmentTabHost tabHost;
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IMApplication.getInstance().ClearAllActivity();
        IMApplication.IS_REFRESH_TOP_MSG = true;
        IMApplication.getInstance().addActivity(this);
//		setContentView(R.layout.tab_menu_activity);
        if (savedInstanceState != null) {
            index = savedInstanceState.getInt("index");
        }
//		tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
//		tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//		tabHost.addTab(tabHost.newTabSpec("talk").setIndicator(createTabView(R.drawable.talk_tab_selector)), TalkListFragment.class, null);
////		tabHost.addTab(tabHost.newTabSpec("contacts").setIndicator(createTabView(R.drawable.contact_tab_selector)), ContactsFragment.class,
////				null);
//		tabHost.addTab(tabHost.newTabSpec("discover").setIndicator(createTabView(R.drawable.discover_tab_selector)), ApplyFragment.class,
//				null);
//
//		tabHost.addTab(tabHost.newTabSpec("me").setIndicator(createTabView(R.drawable.me_tab_selector)), MyFragment.class, null);
//
//		tabHost.setCurrentTab(index);
//		getMyGroups();
    }

    private View createTabView(int id) {
//		View view = View.inflate(this, R.layout.tab_item, null);
////		TextView title = (TextView) view.findViewById(R.id.titleTextView);
//		TextView menuText = (TextView) view.findViewById(R.id.menu_text);
//		if (id == R.drawable.talk_tab_selector) {
//			menuText.setText(getResources().getString(R.string.main_msg));
//
//		} else if (id == R.drawable.contact_tab_selector) {
//			menuText.setText(getResources().getString(R.string.main_contacts));
//
//		} else if (id == R.drawable.discover_tab_selector) {
//			menuText.setText(getResources().getString(R.string.apply));
//
//		} else if (id == R.drawable.me_tab_selector) {
//			menuText.setText(getResources().getString(R.string.main_me));
//
//		}
////		title.setBackgroundResource(id);
//		return view;
        return null;
    }

    /**
     * 获取服务器通讯录数据
     */
    private void getAllFriends() {

        OnGetAllClassmate onGetAllClassmate = new OnGetAllClassmate() {

            @Override
            public void onSucceeful(final GetFriends.getAllFriendResult result) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        boolean is_update = false;
                        if (result.friends != null && result.friends.size() > 0) {
                            FriendDB.updateFriends(result.friends);
                            is_update = true;
                        }
                        if (result.last_update_id > 0) {
                            IMApplication.config.setFriendLastUpdateId(result.last_update_id);
                        }
                        if (is_update) {
                            Intent intent = new Intent(BroadcastAction.ACTION_REFRESH_FRIEND);
                            sendBroadcast(intent);
                        }
                        IMApplication.IS_GET_ALL_FRIENDS = false;
                    }
                }).start();
            }

            @Override
            public void onFail() {

            }
        };
        IMApplication.getAllClassmate(onGetAllClassmate);

    }

    @Override
    protected void onResume() {
        super.onResume();
//		if (IMApplication.IS_GET_ALL_FRIENDS) {
//			// getCustStatus();
//			getAllFriends();
//		}
//		TextView unreadCount = (TextView) tabHost.getTabWidget().getChildTabViewAt(1).findViewById(R.id.unread_count);
//		int count = IMApplication.config.getIsNewFriend();
//
//		if (count > 0) {
//			if (count <= 99) {
//				unreadCount.setText(count + "");
//			} else {
//				unreadCount.setText("99+");
//			}
//			IMApplication.IS_GET_ALL_FRIENDS = false;
//			unreadCount.setVisibility(View.VISIBLE);
//			Intent intent = new Intent(BroadcastAction.SHOW_NEW_FRIENDS_ACTION);
//			intent.putExtra("count", count + "");
//			sendBroadcast(intent);
//		} else {
//			unreadCount.setVisibility(View.GONE);
//		}
//
//		// END
//		setDynamicNewMsg();
//		ShareData.getInstance().setCurPage(IMApplication.PAGE.PAGE_NULL);
//		ShareData.getInstance().setChatId("0");
//		PushServiceConn.getInstance(IMApplication.getAppContext()).startChatService();
    }

    @Override
    protected void onResumeFragments() {
        if (mBroadcastReceiver == null) {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(BroadcastAction.ACTION_FRIENDS_REQUEST);
            intentFilter.addAction(BroadcastAction.MSG_ACTION);
            intentFilter.addAction(BroadcastAction.ACTION_CLEAR_MSG_COUNT);
            intentFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            intentFilter.addAction(BroadcastAction.DISCOVER_UNREAD_COUNT_ACTION);
            mBroadcastReceiver = new updatemsgcountBroadcastReceiver();
            registerReceiver(mBroadcastReceiver, intentFilter);
        }

        super.onResumeFragments();
    }

    public class updatemsgcountBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            final String SYSTEM_DIALOG_REASON_KEY = "reason";
            final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
            final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

            if (intent.getAction().equals(BroadcastAction.ACTION_FRIENDS_REQUEST)) {
                int count = intent.getIntExtra("count", 0);
                if (count > 0) {
                    Message msg = myHandler.obtainMessage();
                    msg.what = 0;
                    msg.arg1 = count;
                    myHandler.sendMessage(msg);
                }
            } else if (intent.getAction().equals(BroadcastAction.MSG_ACTION)) {
                int count = intent.getIntExtra("count", 0);
                String info = intent.getStringExtra("info");

                Message msg = myHandler.obtainMessage();
                msg.what = 1;
                msg.obj = info;
                msg.arg1 = count;
                myHandler.sendMessage(msg);
            } else if (intent.getAction().equals(BroadcastAction.ACTION_CLEAR_MSG_COUNT)) {
                Message msg = myHandler.obtainMessage();
                msg.what = 2;
                myHandler.sendMessage(msg);
            } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                if (reason != null) {
                    if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                        IMApplication.IS_GET_ALL_FRIENDS = true;
                    } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
                        IMApplication.IS_GET_ALL_FRIENDS = true;
                    }
                }
            } else if (intent.getAction().equals(BroadcastAction.DISCOVER_UNREAD_COUNT_ACTION)) {
                setDynamicNewMsg();
            }
        }

    }

    ;

    public Handler myHandler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {
//			if (msg.what == 0) {
//				final TextView unreadCount = (TextView) tabHost.getTabWidget().getChildTabViewAt(1).findViewById(R.id.unread_count);
//				int count = msg.arg1;
//				if (count > 0) {
//					count = IMApplication.config.getIsNewFriend() + count;
//					IMApplication.config.setIsNewFriend(count);
//					if (count <= 99) {
//						unreadCount.setText(count + "");
//					} else {
//						unreadCount.setText("99+");
//					}
//					Intent intent = new Intent(BroadcastAction.SHOW_NEW_FRIENDS_ACTION);
//					intent.putExtra("count", count + "");
//					sendBroadcast(intent);
//					unreadCount.setVisibility(View.VISIBLE);
//				}
//
//			} else if (msg.what == 1) {
//				int count = msg.arg1;
//				final TextView unreadCount = (TextView) tabHost.getTabWidget().getChildTabViewAt(0).findViewById(R.id.unread_count);
//				if (count > 0) {
//					if (count <= 99) {
//						unreadCount.setText(count + "");
//					} else {
//						unreadCount.setText("99+");
//					}
//					unreadCount.setVisibility(View.VISIBLE);
//				} else {
//					unreadCount.setVisibility(View.GONE);
//				}
//			} else if (msg.what == 2) {
//				final TextView unreadCount = (TextView) tabHost.getTabWidget().getChildTabViewAt(0).findViewById(R.id.unread_count);
//				unreadCount.setVisibility(View.GONE);
//			}
            return false;
        }
    });

    @Override
    public void updateTabMsg(TABTYPE tabType, REMINDTYPE remindType, int count) {
        if (tabType == TABTYPE.TAB_CONTACTS) {
            Message msg = myHandler.obtainMessage();
            msg.what = 0;
            msg.arg1 = count;
            myHandler.sendMessage(msg);
        } else if (tabType == TABTYPE.TAB_TALK_MSG) {
            Message msg = myHandler.obtainMessage();
            msg.what = 1;
            msg.arg1 = count;
            myHandler.sendMessage(msg);
        }
    }

    private void getMyGroups() {
        groupMangerHandler.getMyGroups(null);
    }

    private void setDynamicNewMsg() {

//		final TextView unreadCount = (TextView) tabHost.getTabWidget().getChildTabViewAt(2).findViewById(R.id.unread_count);
//		int dynamicUnreadNum = IMApplication.config.getDynamicUnreadMsgCount();
//		String custid = IMApplication.config.getDynamicLastSendCustId();
//		/** 动态最新发布者custId **/
//		/** 动态未读消息数量 **/
//		if (dynamicUnreadNum > 0) {
//			if (dynamicUnreadNum <= 99) {
//				unreadCount.setText(dynamicUnreadNum + "");
//			} else {
//				unreadCount.setText("99+");
//			}
//			unreadCount.setVisibility(View.VISIBLE);
//		} else {
//			unreadCount.setVisibility(View.GONE);
//		}
        // int hasNewDiscovery = 0;
        // if (TextUtils.isEmpty(custid) && dynamicUnreadNum == 0) {
        // hasNewDiscovery = 0;
        // } else if (dynamicUnreadNum != 0) {
        // hasNewDiscovery = dynamicUnreadNum;
        // } else if (!TextUtils.isEmpty(custid)) {
        // hasNewDiscovery = 1;
        // }
        // if (mFragmentCallBack != null) {
        // mFragmentCallBack.updateMsgCount("", hasNewDiscovery);
        // }

    }

    @Override
    protected void onDestroy() {
        if (mBroadcastReceiver != null) {
            unregisterReceiver(mBroadcastReceiver);
        }
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(!isTaskRoot());
        IMApplication.IS_GET_ALL_FRIENDS = true;
    }
}
